package 笔试记录.M8day9用友.用友;


import java.util.ArrayList;


public class Main1 {
    ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
    public int[][] threeOrders (TreeNode root) {
        // write code here
        //这是先序遍历
        lists.add(new ArrayList<>());
        lists.add(new ArrayList<>());
        lists.add(new ArrayList<>());

        preOrder(root,0);

        //这是中序遍历
        inOrder(root,1);

        //这是后序遍历
        postOrder(root,2);
        if(root == null) return new int[3][];
        int len = lists.get(0).size();
        int[][] arr = new int[3][len];
        for (int i = 0; i < 3 ;i++) {
            for (int j = 0; j < len; j++) {
                arr[i][j] = lists.get(i).get(j);
            }
        }
        return arr;
    }
    
    public void preOrder(TreeNode root,int index){
        if(root == null){
            return;
        }
        lists.get(index).add(root.val);
        preOrder(root.left,index);
        preOrder(root.right,index);
    }

    public void inOrder(TreeNode root,int index){
        if(root == null){
            return;
        }
        inOrder(root.left,index);
        lists.get(index).add(root.val);
        inOrder(root.right,index);
    }

    public void postOrder(TreeNode root,int index){
        if(root == null){
            return;
        }
        postOrder(root.left,index);
        postOrder(root.right,index);
        lists.get(index).add(root.val);
    }
}

/*
public int[][] threeOrders (TreeNode root) {
        // write code here
    }
 */
